templatePath = 'template-path/'
# fix CVAS-1394 ,在ie9下，console 未定义
window.console = window.console or do ->
    c = {}
    c.log = c.warn = c.debug = c.info = c.error = c.time = c.dir = c.profile = c.clear = c.exception = c.trace =
      c.assert = ->
    c
angular.module('UCX.Cmis', [
    'LocalStorageModule',
    'ui.bootstrap',
    'pascalprecht.translate'
    'toastr'
    'file-model'
    'ngclipboard'
    'infinite-scroll'
    'ui.bootstrap.contextMenu'
  ]
)
.config ($httpProvider)->
  # 请求截断  暂时仅支持 angularjs 方法 $http的
  $httpProvider.interceptors.push('myInterceptor')
  true

.config ($translateProvider)->
  languageMap={
    DK:'da-DK'
    DE:'de-DE'
    US:'en-US'
    LAS:'en-LAS'
    FI:'fi-FI'
    FR:'fr-FR'
    IT:'it-IT'
    JP:'ja-JP'
    KR:'ko-KR'
    NO:'nb-NO'
    NL:'nl-NL'
    PL:'pl-PL'
    BR:'pt-PB'
    PT:'pt-PT'
    RU:'ru-RU'
    SE:'sv-SE'
    CN:'zh-CN'
    HK:'zh-HK'
    TW:'zh-TW'
  }
  for language, languageData of CmisLanguage
    $translateProvider.translations language, languageData
    $translateProvider.translations languageMap[language], languageData
#    useSanitizeValueStrategy different effects see here http://codepen.io/prophet_w/pen/oYKLed?editors=1010
#    $translateProvider.useSanitizeValueStrategy('escape') // will cause some problem see http://codepen.io/prophet_w/pen/oYKLed?editors=1010

#   if not set browser will warning the safety
    $translateProvider.useSanitizeValueStrategy('escapeParameters')
    
  $translateProvider.fallbackLanguage('US')
.config (toastrConfig)->
  angular.extend toastrConfig, {
#    https://github.com/Foxandxss/angular-toastr    Toastr customization
    positionClass: 'toast-top-center'
    closeButton : true
    escapeHtml : true
    allowHtml: true,
    maxOpened: 5
    preventDuplicates: false
    preventOpenDuplicates: true
#    autoDismiss: false
#    tapToDismiss: false
#    timeOut:0
#    extendedTimeOut:0
  }
.config (localStorageServiceProvider)->
  dataStorage = ''
  try
    if window.localStorage
      if window.localStorage? && typeof window.localStorage != 'undefined'
        dataStorage = 'localStorage'
      else
        dataStorage = 'sessionStorage'
      localStorageServiceProvider.setStorageType(dataStorage)
  catch e
    console.log e

.directive 'cmis', (CmisService, localStorageService, $translate)->
  {
    replace: true
    templateUrl: templatePath + "index.html"
    compile: (tElement, attrs, transclude, $scope)->
      #CBASE-264 日语 用Meiryo UI字体 又改成 Meiryo了 ..
#      console.log('attrs',attrs)
      if attrs.lang=='JP'
        $('#cmis-container').css('font-family','Meiryo')
      if localStorageService.isSupported
        if attrs.lang
          if(attrs.lang.indexOf('-')!=-1)
            attrs.lang = attrs.lang.split('-')[1]
          localStorageService.set "language", attrs.lang
#          if !CmisLanguage[attrs.lang]
#            localStorageService.set "language", 'US'
#          else
#            localStorageService.set "language", attrs.lang
        else
          localStorageService.set "language", 'US'
#        console.log('@@@@@@set lang')
#        console.log(attrs.lang)

        # add debug mode
        cloudReadingDebugMode = localStorageService.get "CloudReadingDebug"
        if !cloudReadingDebugMode
          localStorageService.set("CloudReadingDebug",false)
          console.log = ()->
            return false

      if attrs.url.indexOf("{{") != -1
        return
      else
        CmisService.setConfig(
          attrs.url,
          attrs.token,
          attrs.path,
          attrs.baseUrl,
          attrs.isDrmRoom
          attrs.messageToMember
          attrs.showMembers
          attrs.roomLink
          attrs.previewDid
          attrs.allowExtension
          attrs.repository
          attrs.membershipType
          attrs.allowDownload
          attrs.filePickerModal
          attrs.device
          attrs.pluginVersion
          attrs.fromFillAndSign
          attrs.cwebToolsApi
          attrs.pluginUi
          attrs.fileType
          attrs.fileSelectMode
          attrs.phantomSubscriptionStatus
          attrs.isEnterpriseSite
          attrs.cpdfBaseUrl
          attrs.cpdfDocBaseUrl
          attrs.docOwner
          attrs.project,
          attrs.hideRetireNotify
        )
    controller: 'directiveController'
  }
.directive 'showFocus', ($timeout)->
  (scope, element, attrs)->
    scope.$watch attrs.showFocus, (newValue)->
      $timeout ()->
        newValue && $(element).focus()
      ,
        true